package com.gitee.wsl.collections.iterator


/**
 * Iterates over the first count elements of the iterator or the full size of the iterator
 * if the count is greater than the length of the receiver.
 */
class TakeIterator<T>(
    private val iterator: Iterator<T>,
    private val count: Int
) : Iterator<T> {
    private var currentIndex = 0

    constructor(iterable: Iterable<T>, count: Int) : this(iterable.iterator(), count)

    override fun hasNext(): Boolean {
        return this.currentIndex < this.count && this.iterator.hasNext()
    }

    override fun next(): T {
        if (this.hasNext()) {
            this.currentIndex++
            return this.iterator.next()
        }
        throw NoSuchElementException()
    }

}
